package de.openkanban.client.datasource;

import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceImageField;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.util.Page;

/**
 * Data Source for user contacts. Requests data over HTTP via JSON.
 */
public class UserContactDS extends DataSource {

	private static UserContactDS instance = null;

	public static UserContactDS getInstance() {
		if (instance == null) {
			instance = new UserContactDS("userContactDS");
		}
		return instance;
	}

	private UserContactDS(String id) {
		setID(id);
		DataSourceIntegerField userId = new DataSourceIntegerField("userId");
		userId.setPrimaryKey(true);
		DataSourceTextField vornameField = new DataSourceTextField("vorname", "First Name");
		DataSourceTextField nameField = new DataSourceTextField("nachname", "Last Name");
		DataSourceTextField emailField = new DataSourceTextField("email", "E-Mail");
		DataSourceTextField nkz = new DataSourceTextField("namensKuerzel", " ");

		DataSourceImageField avatar = new DataSourceImageField("avatarUrl", " ", 20);
		avatar.setImageURLPrefix(Page.getAppImgDir() + "avatar/");
		avatar.setImageHeight(20);
		avatar.setImageWidth(20);

		setCacheAllData(false);
		setFields(userId, avatar, vornameField, nameField, emailField, nkz);
		setDataFormat(DSDataFormat.JSON);
		setDataURL("service/user/contact/get");
		setClientOnly(false);
	}

	/**
	 * Override transformRequest. Here for illustration purposes only and
	 * this override implementation simply calls super.transformReRequest
	 * 
	 * @param dsRequest
	 *                the DSRequest being processed
	 * @return the transformed request
	 */
	@Override
	protected Object transformRequest(DSRequest dsRequest) {
		return super.transformRequest(dsRequest);
	}
}
